package de.hs_zigr.inf.iib08gruppe5powerfish.it.control;

import java.io.IOException;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DateAdder for adding current date in front of a given string
 */
public class CtrlDateAdder {

	/**
	 * add current date to string as prefix only once a time
	 * 
	 * @throws IOException
	 */
	public String addDate(String strQuiry) throws IOException {

		Calendar cal = Calendar.getInstance();
		String strDate;
		String strTime;
		strDate = cal.get(Calendar.DAY_OF_MONTH) + "."
				+ (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
		strTime = cal.get(Calendar.HOUR_OF_DAY) + ":"
				+ cal.get(Calendar.MINUTE);
		// to find a date in commentary
		String strRegExp = ".*?\\(:\\s\\d(\\d)?\\.\\d(\\d)?\\.\\d{4,4}\\s\\d(\\d)?:\\d(\\d)?\\s:\\).*?(\n|.*?)*?";
		String strTime_Text;
		Pattern p = Pattern.compile(strRegExp);
		Matcher m = p.matcher(strQuiry);
		// is there still a date in commentary?
		if (m.matches()) {
			// then do not add a further date commentary to the existing date
			// commentary
			strTime_Text = strQuiry;
		} else {
			// add a date commentary
			strTime_Text = "(: " + strDate + " " + strTime + " :) \n"
					+ strQuiry;
		}
		return strTime_Text;
	}
}